package com.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _46_Permute {
    public static void main(String[] args) {
        String a = "1|2|3|4|5";
        String[] str = a.split("\\|");
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
        }
    }
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> track = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        backtrack(nums, track, used);
        return result;
    }

    void backtrack(int[] nums, List<Integer> track, boolean[] used) {
        if (track.size() == nums.length) {
            result.add(new LinkedList<>(track));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            track.add(nums[i]);
            backtrack(nums, track, used);
            track.remove(track.size() - 1);
            used[i] = false;
        }
    }
}
